package heven.holt.library.extensions

import okhttp3.internal.and
import java.util.*

fun String.hexStringToByteArray(): ByteArray {
    val len = length
    val data = ByteArray(len / 2)
    var i = 0
    while (i < len) {
        data[i / 2] = ((Character.digit(this[i], 16) shl 4) + Character.digit(
            this[i + 1], 16
        )).toByte()
        i += 2
    }
    return data
}

fun ByteArray.byteArrayToHexString(): String {
    val sb = StringBuilder(this.size * 2)
    for (element in this) {
        val v: Int = element and 0xff
        if (v < 16) {
            sb.append('0')
        }
        sb.append(Integer.toHexString(v))
    }
    return sb.toString().uppercase(Locale.US)
}